/**************************************************************************
Copyright (C) jianbo miao Corporation.  All Rights Released.
this is a 3d engin named our_3d_engin.
our_3d_engin feature:
1:there is no float.
2:it do the 3d things all by softwear.
3:base on 1 and 2 , it can execution on arm which have no 3d hardwear accelerate.

  if you have any  suggestion or question,pls contact with me
  mail:miaojb@126.com
  msn:miaojianbo@hotmail.com
  qq:30209027
  
    2008/01/01
***************************************************************************/
#ifdef OUR_MAIN_C
#ifndef OUR_DRAW32_C
#define  OUR_DRAW32_C

#include "our_math.h"
#include "our_3d.h"
#include "our_draw32.h"
#include "our_draw16.h"
#include "our_bmp.h"


#if 0
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//==================================================================================
int OUR_Draw_Line32(int x0, int y0, 	// starting position 
                    int x1, int y1, 	// ending position
                    int color,     	// color index
                    UCHAR *vb_start, int lpitch_2) // video buffer and memory pitch
{
    // this function draws a line from xo,yo to x1,y1 using differential error
    // terms (based on Bresenahams work)
    
    int dx,             // difference in x's
        dy,             // difference in y's
        dx2,            // dx,dy * 2
        dy2, 
        x_inc,          // amount in pixel space to move during drawing
        y_inc,          // amount in pixel space to move during drawing
        error,          // the discriminant i.e. error i.e. decision variable
        index;          // used for looping
    UINT *vb_start2;
    lpitch_2 = lpitch_2>>2 ; // USHORT strided lpitch
    
    // pre-compute first pixel address in video buffer based on 32bit data
    vb_start2 = (UINT*)vb_start + x0 + y0*lpitch_2;
    // pre-compute first pixel address in video buffer based on 32bit data
    //UINT*vb_start2 = (UINT*)vb_start + x0 + y0*lpitch_2;
    
    // compute horizontal and vertical deltas
    dx = x1-x0;
    dy = y1-y0;
    
    // test which direction the line is going in i.e. slope angle
    if (dx>=0)
    {
        x_inc = 1;
    } // end if line is moving right
    else
    {
        x_inc = -1;
        dx    = -dx;  // need absolute value
    } // end else moving left
    // test y component of slope
    if (dy>=0)
    {
        y_inc = lpitch_2;
    } // end if line is moving down
    else
    {
        y_inc = -lpitch_2;
        dy    = -dy;  // need absolute value
    } // end else moving up
    
    dx2 = dx << 1;// compute (dx,dy) * 2
    dy2 = dy << 1;
    
    if (dx > dy)// now based on which delta is greater we can draw the line
    {
        
        error = dy2 - dx; // initialize error term
        
        for (index=0; index <= dx; index++)// draw the line
        {
            
            *vb_start2 = (UINT)color;// set the pixel
            
            if (error >= 0) // test if error has overflowed
            {
                error-=dx2;
                
                vb_start2+=y_inc;// move to next line
            } // end if error overflowed
            
            error+=dy2;// adjust the error term
            
            vb_start2+=x_inc;// move to the next pixel
        } // end for
    } // end if |slope| <= 1
    else
    {
        // initialize error term
        error = dx2 - dy; 
        // draw the line
        for (index=0; index <= dy; index++)
        {
            // set the pixel
            *vb_start2 = (UINT)color;
            
            // test if error overflowed
            if (error >= 0)
            {
                error-=dy2;
                // move to next line
                vb_start2+=x_inc;
            } // end if error overflowed
            // adjust the error term
            error+=dx2;
            // move to the next pixel
            vb_start2+=y_inc;
        } // end for
    } // end else |slope| > 1
    // return success
    return(1);
} // end Draw_Line32

                    ///////////////////////////////////////////////////////////
                    
                    ///////////////////////////////////////////////////////////

                    int OUR_Clip_Line(int *x11,int *y11,int *x21, int *y21)
                    {
                        // this function clips the sent line using the globally defined clipping
                        // region
                        
                        // internal clipping codes
#define CLIP_CODE_C  0x0000
#define CLIP_CODE_N  0x0008
#define CLIP_CODE_S  0x0004
#define CLIP_CODE_E  0x0002
#define CLIP_CODE_W  0x0001
                        
#define CLIP_CODE_NE 0x000a
#define CLIP_CODE_SE 0x0006
#define CLIP_CODE_NW 0x0009 
#define CLIP_CODE_SW 0x0005
                        
                        int x1,x2,y1,y2;
                        
                        int xc1,xc2,yc1,yc2;
                        
                        
                        int p1_code=0, 
                            p2_code=0;
                        
                        int min_clip_y=0;
                        int min_clip_x=0;
                        int max_clip_y=OUR_D_screem_h;
                        int max_clip_x=OUR_D_screem_w;
                        xc1=x1; 
                        yc1=y1;
                        xc2=x2; 
                        yc2=y2;
                        x1=*x11; 
                        y1=*y11; 
                        x2=*x21; 
                        y2=*y21;
                        // determine codes for p1 and p2
                        if (y1 < min_clip_y)
                            p1_code|=CLIP_CODE_N;
                        else
                            if (y1 > max_clip_y)
                                p1_code|=CLIP_CODE_S;
                            
                            if (x1 < min_clip_x)
                                p1_code|=CLIP_CODE_W;
                            else
                                if (x1 > max_clip_x)
                                    p1_code|=CLIP_CODE_E;
                                
                                if (y2 < min_clip_y)
                                    p2_code|=CLIP_CODE_N;
                                else
                                    if (y2 > max_clip_y)
                                        p2_code|=CLIP_CODE_S;
                                    
                                    if (x2 < min_clip_x)
                                        p2_code|=CLIP_CODE_W;
                                    else
                                        if (x2 > max_clip_x)
                                            p2_code|=CLIP_CODE_E;
                                        
                                        // try and trivially reject
                                        if ((p1_code & p2_code)) 
                                            return(0);
                                        
                                        // test for totally visible, if so leave points untouched
                                        if (p1_code==0 && p2_code==0)
                                            return(1);
                                        
                                        // determine end clip point for p1
                                        switch(p1_code)
                                        {
                                        case CLIP_CODE_C: break;
                                            
                                        case CLIP_CODE_N:
                                            {
                                                yc1 = min_clip_y;
                                                xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);
                                            } break;
                                        case CLIP_CODE_S:
                                            {
                                                yc1 = max_clip_y;
                                                xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);
                                            } break;
                                            
                                        case CLIP_CODE_W:
                                            {
                                                xc1 = min_clip_x;
                                                yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);
                                            } break;
                                            
                                        case CLIP_CODE_E:
                                            {
                                                xc1 = max_clip_x;
                                                yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
                                            } break;
                                            
                                            // these cases are more complex, must compute 2 intersections
                                        case CLIP_CODE_NE:
                                            {
                                                // north hline intersection
                                                yc1 = min_clip_y;
                                                xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc1 < min_clip_x || xc1 > max_clip_x)
                                                {
                                                    // east vline intersection
                                                    xc1 = max_clip_x;
                                                    yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_SE:
                                            {
                                                // south hline intersection
                                                yc1 = max_clip_y;
                                                xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);	
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc1 < min_clip_x || xc1 > max_clip_x)
                                                {
                                                    // east vline intersection
                                                    xc1 = max_clip_x;
                                                    yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_NW: 
                                            {
                                                // north hline intersection
                                                yc1 = min_clip_y;
                                                xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc1 < min_clip_x || xc1 > max_clip_x)
                                                {
                                                    xc1 = min_clip_x;
                                                    yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);	
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_SW:
                                            {
                                                // south hline intersection
                                                yc1 = max_clip_y;
                                                xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);	
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc1 < min_clip_x || xc1 > max_clip_x)
                                                {
                                                    xc1 = min_clip_x;
                                                    yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);	
                                                } // end if
                                                
                                            } break;
                                            
                                        default:break;
                                            
                                        } // end switch
                                        
                                        // determine clip point for p2
                                        switch(p2_code)
                                        {
                                        case CLIP_CODE_C: break;
                                            
                                        case CLIP_CODE_N:
                                            {
                                                yc2 = min_clip_y;
                                                xc2 = x2 + (min_clip_y-y2)*(x1-x2)/(y1-y2);
                                            } break;
                                            
                                        case CLIP_CODE_S:
                                            {
                                                yc2 = max_clip_y;
                                                xc2 = x2 + (max_clip_y-y2)*(x1-x2)/(y1-y2);
                                            } break;
                                            
                                        case CLIP_CODE_W:
                                            {
                                                xc2 = min_clip_x;
                                                yc2 = y2 + (min_clip_x-x2)*(y1-y2)/(x1-x2);
                                            } break;
                                            
                                        case CLIP_CODE_E:
                                            {
                                                xc2 = max_clip_x;
                                                yc2 = y2 + (max_clip_x-x2)*(y1-y2)/(x1-x2);
                                            } break;
                                            
                                            // these cases are more complex, must compute 2 intersections
                                        case CLIP_CODE_NE:
                                            {
                                                // north hline intersection
                                                yc2 = min_clip_y;
                                                xc2 = x2 + 0.5+(min_clip_y-y2)*(x1-x2)/(y1-y2);
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc2 < min_clip_x || xc2 > max_clip_x)
                                                {
                                                    // east vline intersection
                                                    xc2 = max_clip_x;
                                                    yc2 = y2 + 0.5+(max_clip_x-x2)*(y1-y2)/(x1-x2);
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_SE:
                                            {
                                                // south hline intersection
                                                yc2 = max_clip_y;
                                                xc2 = x2 + 0.5+(max_clip_y-y2)*(x1-x2)/(y1-y2);	
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc2 < min_clip_x || xc2 > max_clip_x)
                                                {
                                                    // east vline intersection
                                                    xc2 = max_clip_x;
                                                    yc2 = y2 + 0.5+(max_clip_x-x2)*(y1-y2)/(x1-x2);
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_NW: 
                                            {
                                                // north hline intersection
                                                yc2 = min_clip_y;
                                                xc2 = x2 + 0.5+(min_clip_y-y2)*(x1-x2)/(y1-y2);
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc2 < min_clip_x || xc2 > max_clip_x)
                                                {
                                                    xc2 = min_clip_x;
                                                    yc2 = y2 + 0.5+(min_clip_x-x2)*(y1-y2)/(x1-x2);	
                                                } // end if
                                                
                                            } break;
                                            
                                        case CLIP_CODE_SW:
                                            {
                                                // south hline intersection
                                                yc2 = max_clip_y;
                                                xc2 = x2 + 0.5+(max_clip_y-y2)*(x1-x2)/(y1-y2);	
                                                
                                                // test if intersection is valid, of so then done, else compute next
                                                if (xc2 < min_clip_x || xc2 > max_clip_x)
                                                {
                                                    xc2 = min_clip_x;
                                                    yc2 = y2 + 0.5+(min_clip_x-x2)*(y1-y2)/(x1-x2);	
                                                } // end if
                                                
                                            } break;
                                            
                                        default:break;
                                            
                                        } // end switch
                                        
                                        // do bounds check
                                        if ((xc1 < min_clip_x) || (xc1 > max_clip_x) ||
                                            (yc1 < min_clip_y) || (yc1 > max_clip_y) ||
                                            (xc2 < min_clip_x) || (xc2 > max_clip_x) ||
                                            (yc2 < min_clip_y) || (yc2 > max_clip_y) )
                                        {
                                            return(0);
                                        } // end if
                                        
                                        // store vars back
                                        *x11 = xc1;
                                        *y11 = yc1;
                                        *x21 = xc2;
                                        *y21 = yc2;
                                        
                                        return(1);
                                        
} // end Clip_Line
//=================================================================================
int OUR_Draw_Clip_Line32(int x0,int y0, int x1, int y1, int color, 
                         UCHAR *dest_buffer, int lpitch)
{
    // this function draws a clipped line
    
    int cxs, cys,
        cxe, cye;
    
    // clip and draw each line
    cxs = x0;
    cys = y0;
    cxe = x1;
    cye = y1;
    
    // clip the line
    if (OUR_Clip_Line(&cxs,&cys,&cxe,&cye))
        OUR_Draw_Line32(cxs, cys, cxe,cye,color,dest_buffer,lpitch);
    
    // return success
    return(1);
    
} // end Draw_Clip_Line32

//--------------------------------------------------------------------

void OUR_Draw_Wire32( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
            OUR_Draw_Clip_Line32(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
            OUR_Draw_Clip_Line32(	point_work[i3].MM.x, 
                point_work[i3].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
            OUR_Draw_Clip_Line32(	point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
            OUR_D_triangle_drawed_num++;
            
        }
    }
} // end Draw_RENDERLIST4DV1_Wire

//===========================================================
//--------------------------------------------------------------------------
#define DRAW_T_SHIFT 20
void OUR_Draw_Triangle_2D32(	int x1,int y1,
                            int x2,int y2,
                            int x3,int y3,
                            CLR color,
                            UCHAR *dest_buffer, int mempitch)
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR *dest;
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
    }					//18t
    
    //mempitch>>=2;//(sizeof(dest)/2);
    dest = (CLR*)dest_buffer +y1*mempitch;		//point to the line of y1
    if(y1<y2 )//the top part
    {		
        ks=((x1-x2)<<DRAW_T_SHIFT)/(y1-y2);		//slope 1 --> 2
        ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);		//slope 1 --> 3
        if(ks>ke)						//start slope must be smaller than the end one
        {
            OUR_SWAP(ks,ke,xs);
        }		
        xs=x1<<DRAW_T_SHIFT;
        xe=xs;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {			
            //y1=xe>>8;
            for(xinc=xs>>DRAW_T_SHIFT;xinc<=(xe>>DRAW_T_SHIFT);xinc++) 		//scan point 
            {
                dest[xinc] =color;		//display
            }
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }		
        if(y2<y3)//the butt part
        {	
            x1=((x2-x3)<<DRAW_T_SHIFT)/(y2-y3);
            if(x1>ke)
            {
                ks=x1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                //y1=xe>>8;
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=(xe>>DRAW_T_SHIFT);xinc++)
                {
                    dest[xinc] =color;
                }
                dest+=mempitch;
                xs+=ks;
                xe+=ke;
            }		
            
        }
        return;
    }
    else
    {		
        if(y2<y3)//the butt part
        {	
            ks=((x2-x3)<<DRAW_T_SHIFT)/(y1-y3);
            ke=((x1-x3)<<DRAW_T_SHIFT)/(y1-y3);
            if(ks<ke)
            {
                OUR_SWAP(ks,ke,xs);
            }	
            if(x1<x2)
            {
                xs=x1<<DRAW_T_SHIFT;
                xe=(x2<<DRAW_T_SHIFT);//+(xei>>1);
            }
            else 
            {
                xs=x2<<DRAW_T_SHIFT;
                xe=(x1<<DRAW_T_SHIFT);//+(xei>>1);
                
            }
            for(yinc=y1;yinc<y3;yinc++)
            {			
                //y1=xe>>8;
                for(xinc=xs>>DRAW_T_SHIFT;xinc<=(xe>>DRAW_T_SHIFT);xinc++)
                {
                    dest[xinc] =color;
                }
                dest+=mempitch;
                xs+=ks;
                xe+=ke;
            }		
        }
        return;
    }
    
}

//--------------------------------------------------------------------------
//------------------------------------				
#define FILL_POINT	{				\
    dest[xinc] =color;	\
    xinc++;			\
}
//----------------------------------------------------------------------------

#define DRAW_P_SHFT_X 18
#define SHIFR_L 18
#define SHIFT 30
//------------------------------------------------------
#define FILE_GOURAUD32	{\
    dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
    ((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);\
    \
    k_r_inc+=kre;\
    k_g_inc+=kge;\
    k_b_inc+=kbe;\
}
//------------------------------------------------------


void OUR_Draw_Gouraud_2D32(	int x1,int y1,
                           int x2,int y2,
                           int x3,int y3,
                           CLR color1,
                           CLR color2,
                           CLR color3,
                           UCHAR *dest_buffer, int mempitch)
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR *dest;
    /*
    x1=200;
    y1=0;
    x2=1;
    y2=200;
    x3=350;
    y3=350;
    
      
        color1 = 0x000000;
        color2 = 0xff0000;
        color3 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK0800)-(color2&OUR_RGB_MASK0800))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK0800)-(color3&OUR_RGB_MASK0800))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0008)-(color2&OUR_RGB_MASK0008)))*xs;
            kbs=(((color1&OUR_RGB_MASK0008)-(color3&OUR_RGB_MASK0008)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK0800)-(color2&OUR_RGB_MASK0800))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK0800)-(color3&OUR_RGB_MASK0800))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0008)-(color2&OUR_RGB_MASK0008)))*xs;
            kbe=(((color1&OUR_RGB_MASK0008)-(color3&OUR_RGB_MASK0008)))*xe;
        }
        else
        {
            return;
        }
#define GND_KKKK 0
        if((ke-ks)<(2<<DRAW_P_SHFT_X))//
        {      
            xs=((1<<(SHIFT-4)))/((ke-ks));
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5-4);
        }
        else
        {
            xs=((1<<(SHIFT)))/((ke-ks)>>GND_KKKK);
            kre=(((kre-krs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kge=(((kge-kgs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);		
            kbe=(((kbe-kbs)>>5)*xs)>>(SHIFT-SHIFR_L-5+GND_KKKK);
        }/**/
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK0800)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0008)<<SHIFR_L;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {	
            k_r_inc=rs;
            k_g_inc=gs;
            k_b_inc=bs;
            //------------------------------------------------------	
            xinc=xs>>(DRAW_P_SHFT_X);
            y1=(xe>>(DRAW_P_SHFT_X));
            dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
                ((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
            for(;xinc<=y1;)	
            {
                FILE_GOURAUD32;
            }	
            rs+=krs;			
            gs+=kgs;			
            bs+=kbs;
            
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK0800)-(color3&OUR_RGB_MASK0800))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0008)-(color3&OUR_RGB_MASK0008)))*y1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                y1=(xe>>(DRAW_P_SHFT_X));
                dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
                    ((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
                for(;xinc<=y1;)	
                {
                    FILE_GOURAUD32;
                }
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK0800)-(color3&OUR_RGB_MASK0800))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK0800)-(color1&OUR_RGB_MASK0800))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0080)-(color1&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0008)-(color3&OUR_RGB_MASK0008)))*xs;
        kbe=(((color2&OUR_RGB_MASK0008)-(color1&OUR_RGB_MASK0008)))*xe;
        
        rs=(color1&OUR_RGB_MASK0800)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0008)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK0800)-(color3&OUR_RGB_MASK0800))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK0800)-(color2&OUR_RGB_MASK0800))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0008)-(color3&OUR_RGB_MASK0008)))*xs;
        kbe=(((color1&OUR_RGB_MASK0008)-(color2&OUR_RGB_MASK0008)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK0800)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0008)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }								
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        dest[xinc++] =((k_r_inc>>(SHIFR_L-16))&OUR_RGB_MASK0800)+\
            ((k_g_inc>>(SHIFR_L-8))&OUR_RGB_MASK0080)+((k_b_inc>>SHIFR_L)&OUR_RGB_MASK0008);
        for(;xinc<=y1;)	
        {
            FILE_GOURAUD32;
        }
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}
//------------------------------------------------------

//------------------------------------------------------
#define FILE_BMP32 	{\
    dest[xinc++] =bmp->bmp_data[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];\
    \
    k_r_inc+=kre;\
    k_g_inc+=kge;\
    k_b_inc+=kbe;\
}
//------------------------------------------------------
void OUR_Draw_BMP_2D32(	int x1,int y1,
                       int x2,int y2,
                       int x3,int y3,
                       CLR color1,
                       CLR color2,
                       CLR color3,
                       UCHAR *dest_buffer, int mempitch,
                       OUR_BMP_INFO* bmp
                       )
{
    int xs,xe,ks,ke,xinc;
    int yinc;
    CLR rs,re,gs,ge,bs,be;
    int krs,kre,kgs,kge,kbs,kbe;
    int  k_r_inc;
    int  k_g_inc;
    int  k_b_inc;
    CLR *dest;
    /*
    x1=10<<1;
    y1=10<<1;
    x2=10<<1;
    y2=265<<1;
    x3=265<<1;
    y3=265<<1;
    
      
        
          color3 = 0x000080;
          color1 = 0x008080;
          color2 = 0x000000;
    */
    //make sure y1<y2<y3
    if(y1>y2)
    {
        OUR_SWAP(y1,y2,xs);
        OUR_SWAP(x1,x2,xs);
        OUR_SWAP(color1,color2,rs);
    }
    if(y1>y3)
    {
        OUR_SWAP(y1,y3,xs);
        OUR_SWAP(x1,x3,xs);
        OUR_SWAP(color1,color3,rs);
    }
    if(y2>y3)
    {
        OUR_SWAP(y2,y3,xs);
        OUR_SWAP(x2,x3,xs);
        OUR_SWAP(color2,color3,rs);
    }								//18t
    
    //mempitch>>=2;					//(sizeof(dest)/2);
    dest = (CLR*)dest_buffer +y1*mempitch;		//point to the line of y1
#if 1
    if(y1<y2 )						//the top part
    {
        xs=(1<<(DRAW_P_SHFT_X))/(y1-y2);
        xe=(1<<(DRAW_P_SHFT_X))/(y1-y3);	
        ks=((x1-x2))*xs;				//slope 1 --> 2
        ke=((x1-x3))*xe;				//slope 1 --> 3
        xs>>=(DRAW_P_SHFT_X-SHIFR_L);
        xe>>=(DRAW_P_SHFT_X-SHIFR_L);
        if(ks>ke)						//start slope must be smaller than the end one
        {
            kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
            OUR_SWAP(ks,ke,xs);
        }	
        else if(ks<ke)	
        {
            krs=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xs;
            kre=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xe;
            
            kgs=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xs;
            kge=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xe;
            
            kbs=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xs;
            kbe=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xe;
        }
        else
        {
            return;
        }
#define BMP_KKKK 5
        //this place must be carefull
        if((ke-ks)<(1<<DRAW_P_SHFT_X))
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L-2)))/((ke-ks));// 28  -- 10 -20 =18 -10
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X) )
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5-2);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10-2);	
            
        }
        else
        {
            xs=((1<<(SHIFT+DRAW_P_SHFT_X-SHIFR_L)))/((ke-ks)>>3);
            //---------------------------------------------
            kre= (kre-krs);
            if(kre>(-(1<<DRAW_P_SHFT_X)) && kre<(1<<DRAW_P_SHFT_X))
                kre=(((kre)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kre=(((kre)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kge= (kge-kgs);
            if(kge>(-(1<<DRAW_P_SHFT_X)) && kge<(1<<DRAW_P_SHFT_X))
                kge=(((kge)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);	
            else
                kge=(((kge)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);
            //----------------------------------------------
            kbe= (kbe-kbs);
            if(kbe>(-(1<<DRAW_P_SHFT_X)) && kbe<(1<<DRAW_P_SHFT_X))
                kbe=(((kbe)>>5)*xs)>>(SHIFT-SHIFR_L-5+3);
            else
                kbe=(((kbe)>>10)*xs)>>(SHIFT-SHIFR_L-10+3);	
        }
        xs=(x1<<(DRAW_P_SHFT_X));
        xe=(x1<<(DRAW_P_SHFT_X));
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);		
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);		
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        for(yinc=y1;yinc<y2;yinc++)		//scan lines
        {	
            k_r_inc=rs;
            k_g_inc=gs;
            k_b_inc=bs;
            //------------------------------------------------------	
            xinc=xs>>(DRAW_P_SHFT_X);
            y1=(xe>>(DRAW_P_SHFT_X));
            dest[xinc++] =bmp->bmp_data[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
            for(;xinc<=y1;)	
            {
                FILE_BMP32;
            }		
            rs+=krs;			
            gs+=kgs;			
            bs+=kbs;
            
            dest+=mempitch;			//point to next line
            xs+=ks;					//next line xs
            xe+=ke;					//next line xe
        }
        if(y2<y3)						//the butt part
        {	
            y1=(1<<(DRAW_P_SHFT_X))/(y2-y3);
            x1=((x2-x3))*y1;
            y1>>=(DRAW_P_SHFT_X-SHIFR_L);
            if(x1>ke)
            {
                ks=x1;
                
                krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*y1;			
                kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*y1;				
                kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*y1;
            }
            else
            {
                ke=x1;
            }
            for(;yinc<=y3;yinc++)
            {			
                k_r_inc=rs;
                k_g_inc=gs;
                k_b_inc=bs;
                //------------------------------------------------------
                xinc=xs>>(DRAW_P_SHFT_X);
                y1=(xe>>(DRAW_P_SHFT_X));
                
                dest[xinc++] =bmp->bmp_data[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
                for(;xinc<=y1;)	
                {
                    FILE_BMP32;
                }	
                rs+=krs;			
                gs+=kgs;			
                bs+=kbs;
                
                dest+=mempitch;			//point to next line
                xs+=ks;					//next line xs
                xe+=ke;					//next line xe
            }		
            
        }
        return;
}
#else

#endif
//---------------------------------------------------------------------
else if(y2<y3)//the butt part
{		
    
    xs=((1<<SHIFR_L))/(y1-y3);	//y1=y2
    ks=((x1-x3))*xs;				//slope 1 --> 2
    ke=((x2-x3))*xs;				//slope 1 --> 3
    if(ks>ke)						//start slope must be smaller than the end one
    {
        xe=((1<<SHIFR_L))/(x2-x1);	
        krs=(((color1&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color2&OUR_RGB_MASK8800)-(color1&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color1&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color2&OUR_RGB_MASK0080)-(color1&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color1&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color2&OUR_RGB_MASK0088)-(color1&OUR_RGB_MASK0088)))*xe;
        
        rs=(color1&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color1&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color1&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xs=x1<<SHIFR_L;
        xe=x2<<SHIFR_L;
    }	
    else if(ks<ke)	
    {
        xe=((1<<SHIFR_L))/(x1-x2);	
        krs=(((color2&OUR_RGB_MASK8800)-(color3&OUR_RGB_MASK8800))>>16)*xs;
        kre=(((color1&OUR_RGB_MASK8800)-(color2&OUR_RGB_MASK8800))>>16)*xe;
        
        kgs=(((color2&OUR_RGB_MASK0080)-(color3&OUR_RGB_MASK0080))>>8)*xs;
        kge=(((color1&OUR_RGB_MASK0080)-(color2&OUR_RGB_MASK0080))>>8)*xe;
        
        kbs=(((color2&OUR_RGB_MASK0088)-(color3&OUR_RGB_MASK0088)))*xs;
        kbe=(((color1&OUR_RGB_MASK0088)-(color2&OUR_RGB_MASK0088)))*xe;
        
        OUR_SWAP(ks,ke,xs);			
        
        rs=(color2&OUR_RGB_MASK8800)<<(SHIFR_L-16);			
        gs=(color2&OUR_RGB_MASK0080)<<(SHIFR_L-8);			
        bs=(color2&OUR_RGB_MASK0088)<<SHIFR_L;
        
        xe=x1<<SHIFR_L;
        xs=x2<<SHIFR_L;
    }
    else
    {
        return;
    }								
    for(yinc=y1;yinc<y3;yinc++)		//scan lines
    {	
        
        k_r_inc=rs;
        k_g_inc=gs;
        k_b_inc=bs;
        //------------------------------------------------------
        
        xinc=xs>>SHIFR_L;
        y1=(xe>>SHIFR_L);
        dest[xinc++] =bmp->bmp_data[((((k_r_inc>>(SHIFR_L))&OUR_RGB_MASK0008)<<bmp->width)+(k_b_inc>>SHIFR_L))];
        for(;xinc<=y1;)	
        {
            FILE_BMP32;
        }
        rs+=krs;			
        gs+=kgs;			
        bs+=kbs;
        
        dest+=mempitch;			//point to next line
        xs+=ks;					//next line xs
        xe+=ke;	
    }		
    return;
}

}


//=======================================================================

void OUR_Draw_flat32( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
            
            OUR_Draw_Triangle_2D32(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                // triangle_work[poly_change].CC_work.clor,
                obj->clor_work[poly_change],
                video_buffer, lpitch);
            
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 
void OUR_Draw_Gouraud32( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
            
            OUR_Draw_Gouraud_2D32(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                obj->clor_work[i1],
                obj->clor_work[i2],
                obj->clor_work[i3],
                video_buffer, lpitch);
            
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 

void OUR_Draw_bmp32( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch)
{
    UINT i1,i2,i3;
    UINT poly;
    UINT num;
    UINT poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;				//how many t would be draw
    for (poly=0; poly <num; poly++)
    {
        //if(OUR_D_FrameTH==obj->pTriangle_changed[poly])
        {
            poly_change=obj->pTriangle_changed[poly];	//whitch t would be draw, see the function backface.....
            i1=triangle_work[poly_change].p1;
            i2=triangle_work[poly_change].p2;
            i3=triangle_work[poly_change].p3;
            
            OUR_Draw_BMP_2D32(point_work[i1].MM.x, 
                point_work[i1].MM.y,
                point_work[i2].MM.x, 
                point_work[i2].MM.y,
                point_work[i3].MM.x, 
                point_work[i3].MM.y,
                triangle_work[poly_change].uv1,
                triangle_work[poly_change].uv2,
                triangle_work[poly_change].uv3,
                video_buffer, lpitch,
                obj->bmp_info);
            
            OUR_D_triangle_drawed_num++;
            
        }
    }
} 

#endif
//------------------------------------------------------
typedef struct OUR_3D_CMP_Z
{
    short  z_order;
    short  meb;
} OUR_3D_CMP_Z;

OUR_3D_CMP_Z  z_cmp[1000];
void OUR_OBJ_CMP_Z(OUR_3D_OBJ_PTR	obj)
{
    UINT i1,i2,i3;
    int poly;
    int num,num2;
    int poly_change;
    FIX_POINT3D_PTR     point_work=obj->pPoints_work;
    OUR_3D_TRAG_PTR      triangle_work=obj->pTriangle;
    num=obj->pTriangle_num_changed;		//how many t would be draw
    num2=0;
    for (poly=0; poly <num; poly++)
    {
        
        poly_change=obj->pTriangle_changed[poly];//whitch t would be draw, see the function backface.....
        i1=triangle_work[poly_change].p1;
        i2=triangle_work[poly_change].p2;
        i3=triangle_work[poly_change].p3;
        z_cmp[poly-num2].z_order=point_work[i1].MM.z+point_work[i2].MM.z+	point_work[i3].MM.z;
        z_cmp[poly-num2].meb=poly_change;
        if(z_cmp[poly-num2].z_order<800)
        {
            num2++;
        }
        else if(z_cmp[poly-num2].z_order>30000)
        {
            num2++;
        }
        else if(point_work[i1].MM.x>2*LCD_W)
        {
            num2++;
        }
        else if(point_work[i1].MM.x<-LCD_W)
        {
            num2++;
        }
        else if(point_work[i1].MM.y>2*LCD_H)
        {
            num2++;
        }
        else if(point_work[i1].MM.y<-LCD_H)
        {
            num2++;
        }
    }
    for (poly=0; poly <num-1-num2; poly++)
    {
        for (i1=poly+1; i1 <num-num2; i1++)
        {
            if(z_cmp[poly].z_order <z_cmp[i1].z_order )
            {
                i2=z_cmp[poly].z_order ;
                z_cmp[poly].z_order=z_cmp[i1].z_order;
                z_cmp[i1].z_order=i2;
                
                i2=z_cmp[poly].meb;
                z_cmp[poly].meb=z_cmp[i1].meb;
                z_cmp[i1].meb=i2;
            }
            
        }
    }
    for (poly=0; poly <num-num2; poly++)
    {
        
        obj->pTriangle_changed[poly]=z_cmp[poly].meb;//whitch t would be draw, see the function backface.....	
    }
    obj->pTriangle_num_changed-= num2;
}


void OUR_Draw_OBJ( OUR_3D_OBJ_PTR	obj,UCHAR *video_buffer, int lpitch,int clor_mod)
{
    if(obj->material &MATERIAL_cmp_z)
    { 
        OUR_OBJ_CMP_Z(obj);
    }
    if(clor_mod==CLOR_MOD_8888)
    {
#if 0
        switch(obj->material &MATERIAL_MASK)
        {
        case MATERIAL_point_1P:
        case MATERIAL_point_1:
            OUR_Draw_Gouraud32(obj,video_buffer,lpitch);
            return;
        case MATERIAL_point_3T:
            //OUR_Draw_Gouraud32(obj,video_buffer,lpitch);
            return;
        case MATERIAL_Triangle_T:
        case MATERIAL_Triangle_1:
            OUR_Draw_flat32(obj,video_buffer,lpitch);
            return;
        case MATERIAL_wire:
            OUR_Draw_Wire32(obj,video_buffer,lpitch);
            return;
        case MATERIAL_bmp:
            OUR_Draw_bmp32(obj,video_buffer,lpitch);
            return;
        default:
            return;
        }
#endif
    }
    
    else if(clor_mod==CLOR_MOD_565)
    {
        switch(obj->material &MATERIAL_MASK)
        {
        case MATERIAL_point_1P:
        case MATERIAL_point_1:
            OUR_Draw_Gouraud16(obj,video_buffer,lpitch);
            return;
        case MATERIAL_Triangle_T:
        case MATERIAL_Triangle_1:
            OUR_Draw_flat16(obj,video_buffer,lpitch);
            return;
        case MATERIAL_wire:
            //	OUR_Draw_Wire16(obj,video_buffer,lpitch);
            return;
        case MATERIAL_bmp:
            OUR_Draw_bmp16(obj,video_buffer,lpitch);
            return;
        default:
            return;
        }
    }
    
} 

//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
//FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
#endif
#endif

